/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.enforcement.integral.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.enforcement.integral.dto.RiskIntegralExchangeDTO;
import com.hopes.bizz.enforcement.integral.dto.RiskIntegralGoodsExcelDTO;
import com.hopes.bizz.enforcement.integral.entity.HazIntegralGoodsEntity;
import com.hopes.bizz.enforcement.integral.service.EnfIntegralGoodsService;
import com.hopes.bizz.enforcement.integral.vo.RiskIntegralGoodsExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 积分商品管理
 *
 * @author liujiaqi
 * @date 2023-07-25 14:39:19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/enfIntegralGoods" )
@Tag(description = "hazIntegralGoods" , name = "积分商品管理管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class EnfIntegralGoodsController {

    private final EnfIntegralGoodsService enfIntegralGoodsService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param riskIntegralGoods 积分商品管理
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('basic_integral_goods_view')" )
    public R getHazIntegralGoodsPage(Page page, HazIntegralGoodsEntity riskIntegralGoods) {
        LambdaQueryWrapper<HazIntegralGoodsEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Objects.nonNull(riskIntegralGoods.getStatus()), HazIntegralGoodsEntity::getStatus,riskIntegralGoods.getStatus());
        wrapper.like(StrUtil.isNotBlank(riskIntegralGoods.getGoodsName()), HazIntegralGoodsEntity::getGoodsName,riskIntegralGoods.getGoodsName());
        wrapper.like(StrUtil.isNotBlank(riskIntegralGoods.getGoodsNumber()), HazIntegralGoodsEntity::getGoodsNumber,riskIntegralGoods.getGoodsNumber());
		wrapper.eq(Objects.nonNull(riskIntegralGoods.getExchangeIntegral()), HazIntegralGoodsEntity::getExchangeIntegral, riskIntegralGoods.getExchangeIntegral());
        return R.ok(enfIntegralGoodsService.page(page, wrapper));
    }


    /**
     * 通过id查询积分商品管理
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('basic_integral_goods_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(enfIntegralGoodsService.getById(id));
    }

    /**
     * 新增积分商品管理
     * @param riskIntegralGoods 积分商品管理
     * @return R
     */
    @Operation(summary = "新增积分商品管理" , description = "新增积分商品管理" )
    @SysLog("新增积分商品管理" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('basic_integral_goods_add')" )
    public R save(@RequestBody HazIntegralGoodsEntity riskIntegralGoods) {
        return R.ok(enfIntegralGoodsService.save(riskIntegralGoods));
    }

    /**
     * 修改积分商品管理
     * @param riskIntegralGoods 积分商品管理
     * @return R
     */
    @Operation(summary = "修改积分商品管理" , description = "修改积分商品管理" )
    @SysLog("修改积分商品管理" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('basic_integral_goods_edit')" )
    public R updateById(@RequestBody HazIntegralGoodsEntity riskIntegralGoods) {
        return R.ok(enfIntegralGoodsService.updateById(riskIntegralGoods));
    }

    /**
     * 通过id删除积分商品管理
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除积分商品管理" , description = "通过id删除积分商品管理" )
    @SysLog("通过id删除积分商品管理" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('basic_integral_goods_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(enfIntegralGoodsService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 积分商品兑换
     * @return R
     */
    @Operation(summary = "积分商品兑换" , description = "积分商品兑换" )
    @SysLog("积分商品兑换" )
    @PostMapping("/exchange")
    @PreAuthorize("@pms.hasPermission('basic_integral_goods_edit')" )
    public R exchange(@RequestBody RiskIntegralExchangeDTO riskIntegralExchangeDTO) {
        return R.ok(enfIntegralGoodsService.exchange(riskIntegralExchangeDTO));
    }



    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('basic_integral_goods_export')")
    public List<RiskIntegralGoodsExcelVO> template() {
        return enfIntegralGoodsService.exportExcel(Collections.singletonList(new HazIntegralGoodsEntity()));
    }

    /**
     * 导出excel 表格
     * @param riskIntegralGoodsExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('basic_integral_goods_export')")
    public List<RiskIntegralGoodsExcelVO> export(RiskIntegralGoodsExcelDTO riskIntegralGoodsExcelDto) {
        return enfIntegralGoodsService.exportExcel(enfIntegralGoodsService.list());
    }

    /**
     * 导入excel 表格
     * @param riskIntegralGoodsExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('basic_integral_goods_export')")
    public R importExcel(@RequestExcel List<RiskIntegralGoodsExcelDTO> riskIntegralGoodsExcelDtoList, BindingResult bindingResult) {
        return enfIntegralGoodsService.importExcel(riskIntegralGoodsExcelDtoList, bindingResult);
    }
}
